﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Database.BLLs;
using QLK.Web.App_Code;
using QLK.Database;

namespace QLK.Web
{
    public partial class ImportInvoiceManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridImportInvoices.AllowPaging = true;
            GridImportInvoices.PageSize = 4;
            if (IsPostBack == false)
            {
                LoadAllImportInvoice();
            }
        }
        int i = 1;
        public int Get_STT()
        {
            return i++;   // mỗi dòng cho lên là 1 
        }

        protected void GridImportInvoices_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridImportInvoices.PageIndex = e.NewPageIndex;
            int trang_thu = e.NewPageIndex;
            int so_dong = GridImportInvoices.PageSize;

            i = trang_thu * so_dong + 1;
            LoadAllImportInvoice();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.ImportInvoicesDetails);
        }

        protected void btnAdd2_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.ImportInvoicesDetails);
        }
        private void LoadAllImportInvoice()
        {
            ImportInvoiceBLL importBLL = new ImportInvoiceBLL();
            GridImportInvoices.DataSource = importBLL.GetAllImportInvoice();
            GridImportInvoices.DataBind();
           
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ImportInvoice import = new ImportInvoice();
            ImportInvoiceBLL importBLL = new ImportInvoiceBLL();
            string invoiceID;
            foreach (GridViewRow row in GridImportInvoices.Rows)
            {
                CheckBox checkbox = (CheckBox)row.FindControl("chkSelect");
                if (checkbox.Checked)
                {
                    invoiceID = GridImportInvoices.DataKeys[row.RowIndex].Value.ToString();
                    import.InvoceID = invoiceID;

                    importBLL.DeleteImportInvoiceByImportID(import);
                }
            }
            LoadAllImportInvoice();
           
        }

        protected void ImageEdit_Click(object sender, ImageClickEventArgs e)
        {
            GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
            int index = gvRow.RowIndex;

            ImportInvoice import = new ImportInvoice();
            import.InvoceID = GridImportInvoices.Rows[index].Cells[1].Text.ToString();
            import.StorageID =int.Parse(GridImportInvoices.Rows[index].Cells[2].Text.ToString());
            //import.ImportDate=DateTime.(GridImportInvoices.Rows[index].Cells[3].Text.ToString());
            import.SupplierID = int.Parse(GridImportInvoices.Rows[index].Cells[4].Text.ToString());
            import.EmployeeID = int.Parse(GridImportInvoices.Rows[index].Cells[5].Text.ToString());
            //import.TotalValues =int.Parse(GridImportInvoices.Rows[index].Cells[6].Text);
            ImageButton img = sender as ImageButton;

            Response.Redirect(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}&{9}={10}",PageUrls.ImportInvoicesDetails,
                Columns.ImportID,img.CommandArgument,Columns.InvoiceID,import.InvoceID,
                Columns.StorageID,import.StorageID,Columns.SupplierID,import.SupplierID,
                Columns.EmployeeID,import.EmployeeID));
        }
    }
}
